﻿/// <reference name="MicrosoftAjax.js"/>
/// <reference path="~/Clients/Scripts/jQuery/jquery-1.6.1-vsdoc.js"/>
/// <reference path="~/Clients/Scripts/utils.debug.js"/>
/// <reference name="Telerik.Web.UI.Common.Core-vsdoc.js" assembly="Telerik.Web.UI" />

Function.prototype.setCtx = function(ctx, opt) {
    var func = this;
    if (GreeNova.Utils.isNoU(opt)) {
        if (GreeNova.Utils.isNNoU(ctx)) {
            return function() {
                if (arguments.length > 0)
                    return func.apply(ctx, arguments);
                return func.call(ctx);
            }
        }
        return func;
    }

    return function() {
        var c = ctx;
        var o = opt;

        var a = [];
        var ai = 0;
        var l, i;

        if (typeof o.fargs === 'undefined' && typeof o.largs === 'undefined' && typeof o.ctxfarg === 'undefined' && typeof o.ctxlarg === 'undefined' && (typeof o.passArgs === 'undefined' || o.passArgs)) {
            a = arguments;
        }
        else {
            if (o.ctxfarg) {
                a[ai++] = this;
            }
            if (typeof o.fargs !== 'undefined') {
                var argsFirst = o.fargs;
                if (argsFirst instanceof Array) {
                    l = argsFirst.length;
                    for (i = 0; i < l; i++)
                        a[ai++] = argsFirst[i];
                }
                else
                    a[ai++] = argsFirst;
            }
            if (typeof o.passArgs === 'undefined' || o.passArgs) {
                if (arguments.length > 0) {
                    l = arguments.length;
                    for (i = 0; i < l; i++)
                        a[ai++] = arguments[i];
                }
            }
            if (typeof o.largs !== 'undefined') {
                var argsLast = o.largs;
                if (argsLast instanceof Array) {
                    l = argsLast.length;
                    for (i = 0; i < l; i++)
                        a[ai++] = argsLast[i];
                }
                else
                    a[ai++] = argsLast;
            }
            if (o.ctxlarg) {
                a[ai++] = this;
            }
        }

        if (typeof o.epd !== 'undefined' && o.epd) {
            var e;
            if (arguments.length > 0 && typeof (e = arguments[0]).preventDefault === 'function')
                e.preventDefault();
        }
        if (typeof o.esp !== 'undefined' && o.epd) {
            if (arguments.length > 0 && typeof (e = arguments[0]).stopPropagation === 'function')
                e.stopPropagation();
        }

        if (a.length > 0) {
            if (c !== undefined)
                return func.apply(c, a);
            return func.apply(this, a);
        }
        if (c !== undefined)
            return func.call(c);
        return func.call(this);
    }
};

(function() {
    var textOrig = jQuery.fn.text;
    jQuery.fn.text = function(text) {
        if (text === null)
            text = '';
        return textOrig.apply(this, arguments);
    };
})();
